using imitateMacQQ.WebApi.Configures;
using Newtonsoft.Json;

namespace imitateMacQQ.WebApi.Repository.Redis;

/// <summary>
/// redis序列string-tools
/// </summary>
public static class SerializeTools
{
    /// <summary>
    /// 序列化
    /// </summary>
    /// <param name="data">要存储redis的对象</param>
    /// <typeparam name="T">对象类型</typeparam>
    /// <returns>序列化string </returns>
    public static string Serialization<T>(T data) where T : new()
    {
        return JsonConvert.SerializeObject(data, Formatting.Indented);
    }

    /// <summary>
    /// 序列化
    /// </summary>
    /// <param name="data">redis读取出的字符串</param>
    /// <param name="result">解析出的类</param>
    /// <typeparam name="T">type</typeparam>
    /// <returns>解析出来的对象</returns>
    public static bool DeSerialization<T>(string data, out T? result)
        where T : new()
    {
        result = default(T);
        if (string.IsNullOrEmpty(data))
            return false;
        try
        {
            result = JsonConvert.DeserializeObject<T>(data);
            return true;
        }
        catch
        {
            Console.WriteLine();
        }

        return false;
    }
}